package us.codecraft.webmagic.scheduler;

import java.util.Date;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;

public class MongoSchedulePlanner implements SchedulePlanner{
	public static final String TimeStampKey = "time";
	public static final String PrimaryKey = "url";
	public static final long Days = 86400000;
	
	Mongo mc;
	DB db;
	DBCollection coll;
	
	DBCursor cursor;
	DBObject entry;
	
	Date date;
	
	public MongoSchedulePlanner (Mongo mc,DBCollection Collection) {
		date = new Date();
		
		BasicDBObject query = new BasicDBObject();
		query.put("visited", "1");
		
		cursor = coll.find(query);
		if (cursor.hasNext()) entry = cursor.next();
	}
	
	@Override
	public void setRevisitable() {
		// TODO Auto-generated method stub
		
	}
	
	public void setRevisitable(DBObject entry) {
		// TODO Auto-generated method stub
		coll.update(entry, new BasicDBObject().append("visited", "0"));
	}

	@Override
	public boolean Revisitable() {
		// TODO Auto-generated method stub
		long x = Long.valueOf(entry.get(TimeStampKey).toString());
		if ((date.getTime() - x) >  Days) {
			return true;
		}else{
			return false;
		}
	}


}
